﻿using System.Collections.Generic;
using System.Linq;
using DreamDriven.ObjectModel;
using NHibernate;
using Rhino.Mocks;

namespace DreamDriven.TestFramework
{
    public abstract class BaseDomainRepositoryTest<T, TDataObject, TIdentifier>
        where T : DomainRepository<TDataObject, TIdentifier>
        where TDataObject : class, IDataObject
    {
        protected readonly MockRepository MockRepository = new MockRepository();
        protected T DomainRepository { get; private set; }
        private IDomainModel _domainModel;

        public virtual void Initialize()
        {
            _domainModel = MockRepository.DynamicMock<IDomainModel>();
            DomainRepository = MockRepository.PartialMock<T>(_domainModel);
            DomainRepository.Stub(r => r.Collection).Return(Collection.AsQueryable());

            ISession session = MockRepository.DynamicMock<ISession>();
            ITransaction transaction = MockRepository.DynamicMock<ITransaction>();
            session.Stub(s => s.BeginTransaction()).Return(transaction);
            session.Stub(s => s.Transaction).Return(transaction);
            DomainRepository.Stub(dr => dr.Session).Return(session);
        }
     
        protected abstract IEnumerable<TDataObject> Collection { get; }
    }

}
